﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

#if  UNITY_IOS&&!DEVELOP
using System.Runtime.InteropServices;
#endif

namespace QQSDK
{
	public class AnalyticsManager
	{
        //public AndroidJavaObject AJO = SDKManager.instance.UnityActivity;
#if UNITY_EDITOR || DEVELOP

#elif UNITY_IOS && !DELSDK
         [DllImport("__Internal")]
         private static extern void logPageViewBeginEventWithPage(string page,string args);
         [DllImport("__Internal")]
         private static extern void logPageViewBeginEvent(string action,string fArg);
         [DllImport("__Internal")]
         private static extern void logPageViewEndEventWithPageWithName(string page,string shouldClearCurrentPageName,string args);
         [DllImport("__Internal")]
         private static extern void logPageViewEndEventWithPage(string page,string args);
         [DllImport("__Internal")]
         private static extern void logPageViewEndEvent(string action,string fArg);
         [DllImport("__Internal")]
         private static extern void logClickEventWithPage(string page,string container,string action,string args);
         [DllImport("__Internal")]
         private static extern void logClickEvent(string action,string fArg);
         [DllImport("__Internal")]
         private static extern void logTaskEventWithPage(string page,string container,string action,bool status,string args);
         [DllImport("__Internal")]
         private static extern void logTaskEvent(string action,string fArg);
         [DllImport("__Internal")]
         private static extern void logShowEventWithPage(string page,string container,string action,string args);
         [DllImport("__Internal")]
         private static extern void logShowEvent(string action,string fArg);
         [DllImport("__Internal")]
         private static extern void setUserProperty(string userProperty,bool once);
         [DllImport("__Internal")]
         private static extern void addUserProperty(string userProperty);
         [DllImport("__Internal")]
         private static extern void updateUserProperty();
         [DllImport("__Internal")]
         private static extern void resetSession();
         [DllImport("__Internal")]
         private static extern void increaseRewardShowCount();
         [DllImport("__Internal")]
         private static extern void increaseInterstitialShowCount();
         [DllImport("__Internal")]
         private static extern void increaseNativeShowCount();
         [DllImport("__Internal")]
         private static extern void increaseBannerShowCount();
         [DllImport("__Internal")]
         private static extern void increaseSplashShowCount();
         [DllImport("__Internal")]
         private static extern void interspageShowCount();
         [DllImport("__Internal")]
         private static extern string getDeviceId();
         [DllImport("__Internal")]
         private static extern void setUserId(string user_id);
         [DllImport("__Internal")]
         private static extern void recordUserIsFcmRealName(string is_fcm_realname);
         [DllImport("__Internal")]
         private static extern void recordUserWorksCount(int works_count);
         [DllImport("__Internal")]
         private static extern void recordUserTaskCount(int task_count);
         [DllImport("__Internal")]
         private static extern void recordUserLevelCount(int level_count);
         [DllImport("__Internal")]
         private static extern void logFirebaseEvent(string eventName, string jsonParams);
         [DllImport("__Internal")]
         private static extern void setThinkTrackEvent(string eventName, string name);
         [DllImport("__Internal")]
         private static extern string getThinkingDataDistinctId();
         [DllImport("__Internal")]
         private static extern void setAttributionType(string type);
         [DllImport("__Internal")]
         private static extern void logEventArgs(string action,string fArg);
         
         
        //[DllImport("__Internal")]
        //private static extern void setReYunId(string ryid);
        //[DllImport("__Internal")]
        //private static extern string getReYunId();
        [DllImport("__Internal")]
        private static extern string getAfId();

#elif UNITY_ANDROID
         public static AndroidJavaObject AJO;
#endif

        public static void Set(){
#if UNITY_EDITOR || DEVELOP

#elif UNITY_IOS && !DELSDK

#elif UNITY_ANDROID
        AJO = SDKManager.SDK_Analytics;
#endif
        }

        #region 用户普通事件

        //启动事件，一般不用处理
        public static void LogLaunchWithType(string type,string reason, ArrayList args)
        {
            string fArg;
            if(args == null || args.Count == 0)
            {
                fArg = "";
            }
            else
            {
     
                fArg = JsonMapper.ToJson(args);
            }
#if UNITY_EDITOR || DEVELOP
            SDKFunc.Log("kkkkk"+ fArg);
#elif UNITY_IOS && !DELSDK

#elif UNITY_ANDROID
        if (AJO  != null){
            AJO.Call("logLaunchEventWithType",type,reason,fArg);
        }
#endif
        }

        //自定义事件 不需数仓加工，即可显示
        public static void SetThinkTrackEvent(string eventName, Dictionary<string, object> dict = null)
        {

            if (string.IsNullOrEmpty(eventName))
                return;

            if (dict != null && dict.Count > 0)
            {
               
#if UNITY_EDITOR || DEVELOP
                SDKFunc.Log("SetThinkTrackEvent:" + eventName + System.Environment.NewLine + "Params:" + JsonMapper.ToJson(dict));
                dict.Clear();
#elif UNITY_IOS && !DELSDK

                ArrayList args = new ArrayList();
                args.Add(dict);
                logEventArgs(eventName, JsonMapper.ToJson(args));
                dict.Clear();
                args.Clear();
            
#elif UNITY_ANDROID
                if (AJO != null)
                {
                    ArrayList args = new ArrayList();
                    args.Add(dict);
                    AJO.Call("logThinkingDataEventArgs", eventName ,JsonMapper.ToJson(args));
                    dict.Clear();
                    args.Clear();
                }
#endif



            }
            else
            {
#if UNITY_EDITOR || DEVELOP
                SDKFunc.Log("SetThinkTrackEvent:" + eventName + System.Environment.NewLine + "Params:");
#elif UNITY_IOS && !DELSDK
                setThinkTrackEvent(eventName, "");
#elif UNITY_ANDROID
                if (AJO != null)
                {
                    AJO.Call("logThinkingDataEventArgs", eventName, "");
                }
#endif
            }


        }

        //数仓事件 - 需要数仓加工，才能在分析平台中显示（常用于关键事件）
        public static void LogTaskEventWithPageNew(string page, string container, string action, bool status, ArrayList args)
        {
            string fArg;
            if(args == null || args.Count == 0)
            {
                fArg = "";
            }
            else
            {
                fArg = JsonMapper.ToJson(args);
            }
            SDKFunc.Log("logTaskEventWithPage:" + page + " container=" + container +"TaskArgs:" + fArg);
#if UNITY_EDITOR || DEVELOP
#elif UNITY_IOS && !DELSDK
            logTaskEventWithPage(page,container,action,status,fArg);
#elif UNITY_ANDROID
            if (AJO  != null){
                AJO.Call("logTaskEventWithPage",page,container,action,status,fArg);
            }
#endif
        }

        //数仓事件 - 需要数仓加工，才能在分析平台中显示（常用于关键事件）-2023
        public static void LogTaskEvent(string action, ArrayList args)
        {
            string fArg;
            if(args == null || args.Count == 0)
            {
                fArg = "";
            }
            else
            {
                fArg = JsonMapper.ToJson(args);
            }
            SDKFunc.Log("LogTaskEvent: action = " + action + "," + fArg + "");
#if UNITY_EDITOR || DEVELOP
#elif UNITY_IOS && !DELSDK
            logTaskEvent(action,fArg);
#elif UNITY_ANDROID
        if (AJO  != null){
            AJO.Call("logTaskEvent",action,fArg);
        }
#endif
        }


        #endregion

        #region 统计页面时长打点
        //页面打开 计时开始
        public static void LogPageViewBeginEventWithPage(string page,ArrayList args){
            string fArg;
            if(args == null || args.Count == 0)
            {
                fArg = "";
            }
            else
            {
                fArg = JsonMapper.ToJson(args);
            }
#if UNITY_EDITOR || DEVELOP

#elif UNITY_IOS && !DELSDK
            logPageViewBeginEventWithPage(page,fArg);
#elif UNITY_ANDROID
        if (AJO  != null){
            AJO.Call("logPageViewBeginEventWithPage",page,fArg);
        }
#endif
        }
        //页面关闭 计时结束
        public static void LogPageViewEndEventWithPage(string page,ArrayList args){
            string fArg;
            if(args == null || args.Count == 0)
            {
                fArg = "";
            }
            else
            {
                fArg = JsonMapper.ToJson(args);
            }
#if UNITY_EDITOR || DEVELOP

#elif UNITY_IOS && !DELSDK
            logPageViewEndEventWithPage(page,fArg);
#elif UNITY_ANDROID
            if (AJO  != null){
                AJO.Call("logPageViewEndEventWithPage",page,fArg);
            }
#endif
        }

        //页面打开 计时开始 -2023
        public static void LogPageViewBeginEvent(string action, ArrayList args)
        {
            string fArg;
            if(args == null || args.Count == 0)
            {
                fArg = "";
            }
            else
            {
                fArg = JsonMapper.ToJson(args);
            }
            SDKFunc.Log("LogPageViewBeginEvent:action="+ action + "," +fArg + "");
#if UNITY_EDITOR || DEVELOP

#elif UNITY_IOS && !DELSDK
            logPageViewBeginEvent(action,fArg);
#elif UNITY_ANDROID
            if (AJO  != null){
                AJO.Call("logPageViewBeginEvent",action,fArg);
            }
#endif
        }
        //页面关闭 计时结束 -2023
        public static void LogPageViewEndEvent(string action, ArrayList args)
        {
            string fArg;
            if(args == null || args.Count == 0)
            {
                fArg = "";
            }
            else
            {
                fArg = JsonMapper.ToJson(args);
            }
            SDKFunc.Log("LogPageViewEndEvent:action=" + action + "," + fArg + "");
#if UNITY_EDITOR || DEVELOP

#elif UNITY_IOS && !DELSDK
            logPageViewEndEvent(action,fArg);
#elif UNITY_ANDROID
            if (AJO  != null){
                AJO.Call("logPageViewEndEvent",action,fArg);
            }
#endif
        }

        #endregion

        #region 广告统计事件
        //广告点击事件
        public static void LogClickEventWithPage(string page,string container,string action,ArrayList args){
            string fArg;
            if(args == null || args.Count == 0)
            {
                fArg = "";
            }
            else
            {
                fArg = JsonMapper.ToJson(args);
            }
            SDKFunc.Log("logClickEventWithPage:" + page + " container=" + container + "fArg = "+fArg);
#if UNITY_EDITOR || DEVELOP

#elif UNITY_IOS && !DELSDK
            logClickEventWithPage(page,container,action,fArg);
#elif UNITY_ANDROID
            if (AJO  != null){
                AJO.Call("logClickEventWithPage",page,container,action,fArg);
            }
#endif
        }
        //广告关闭事件
        public static void LogShowEventWithPage(string page,string container,string action,ArrayList args){
            string fArg;
            if(args == null || args.Count == 0)
            {
                fArg = "";
            }
            else
            {
                fArg = JsonMapper.ToJson(args);
            }
            SDKFunc.Log("LogShowEventWithPage:" + page + " container=" + container + "fArg = "+fArg);
#if UNITY_EDITOR || DEVELOP

#elif UNITY_IOS && !DELSDK
            logShowEventWithPage(page,container,action,fArg);
#elif UNITY_ANDROID
            if (AJO  != null){
                AJO.Call("logShowEventWithPage",page,container,action,fArg);
            }
#endif
        }

        //广告点击事件 -2023
        public static void LogClickEvent(string action, ArrayList args)
        {
            string fArg;
            if(args == null || args.Count == 0)
            {
                fArg = "";
            }
            else
            {
                fArg = JsonMapper.ToJson(args);
            }
            SDKFunc.Log("logClickEvent:" + action + " container=" + fArg);
#if UNITY_EDITOR || DEVELOP

#elif UNITY_IOS && !DELSDK
            logClickEvent(action,fArg);
#elif UNITY_ANDROID
            if (AJO  != null){
                AJO.Call("logClickEvent",action,fArg);
            }
#endif
        }
        //广告关闭事件 -2023
        public static void LogShowEvent(string action, ArrayList args)
        {
            string fArg;
            if(args == null || args.Count == 0)
            {
                fArg = "";
            }
            else
            {
                fArg = JsonMapper.ToJson(args);
            }
            SDKFunc.Log("LogShowEvent:" + action + " container = " +fArg);
#if UNITY_EDITOR || DEVELOP

#elif UNITY_IOS && !DELSDK
            logShowEvent(action,fArg);
#elif UNITY_ANDROID
        if (AJO  != null){
            AJO.Call("logShowEvent",action,fArg);
        }
#endif
        }
        #endregion

        #region 用户属性
        //设置用户属性
        public static void SetUserProperty(string userProperty,bool once = false){
            SDKFunc.Log("SetUserProperty:" + userProperty + " once="+ once);
#if UNITY_EDITOR || DEVELOP

#elif UNITY_IOS && !DELSDK
            setUserProperty(userProperty,once);
#elif UNITY_ANDROID
        if (AJO  != null)
            AJO.Call("setUserProperty",userProperty,once);
#endif
        }
        //用户属性累加
        public static void AddUserProperty(string userPropertyJson)
        {
            SDKFunc.Log("AddUserProperty:" + userPropertyJson + "");
#if UNITY_EDITOR || DEVELOP

#elif UNITY_IOS && !DELSDK
                    addUserProperty(userPropertyJson);
#elif UNITY_ANDROID
                if (AJO  != null)
                    AJO.Call("addUserProperty",userPropertyJson);
#endif
        }

        #endregion

        //设置归因类型（af或reyun）
        public static void SetAttributionType(string type)
        {
#if UNITY_EDITOR || DEVELOP

#elif UNITY_IOS && !DELSDK
            setAttributionType(type); //iso 添加
#elif UNITY_ANDROID
        if (AJO  != null)
            AJO.Call("setAttributionType",type);
#endif
        }


        //从数仓保存对象获取AFid
        public static string getAFid()
        {
            SDKFunc.Log("getAFid");
            string afId = string.Empty;
#if UNITY_EDITOR || DEVELOP

#elif UNITY_IOS && !DELSDK
        return getAfId();
#elif UNITY_ANDROID
        if (AJO  != null)
            afId = AJO.Call<string>("getAfId");
        else
            return afId;
#endif
            return afId;
        }

        public static string getThinkDataDistinctId()
        {
            return GetDistinctId();
        }
        public static string GetDistinctId()
        {
            string distinctId = string.Empty;
#if UNITY_EDITOR || DEVELOP

#elif UNITY_IOS && !DELSDK
            distinctId = getThinkingDataDistinctId();
#elif UNITY_ANDROID
        if (AJO  != null)
            distinctId = AJO.Call<string>("getDistinctId");
        else
            return distinctId;
#endif
            return distinctId;
        }


        /// <summary>
        /// 获取af id  网赚接口验证可用
        /// </summary>
        /// <param name="type"></param>
        public static string GetAFID()
        {
            string id = "";
#if UNITY_EDITOR || DEVELOP

#elif UNITY_IOS && !DELSDK
#elif UNITY_ANDROID
        if (AJO  != null)
           id = AJO.Call<string>("getAfId");
#endif
            return id;
        }

        #region google 订阅和内购汇报
        public static void logGooglePlaySubscribe(string iap_id, string date_type, double price, string currency, string source)
        {
#if UNITY_EDITOR || DEVELOP

#elif UNITY_IOS && !DELSDK

#elif UNITY_ANDROID
                if (AJO  != null)
                    AJO.Call("subscribeAppContent", iap_id,date_type,price,currency,source);
#endif
        }

        public static void logGooglePlayIAP(string iap_id, double price, string currency, string source)
        {
#if UNITY_EDITOR || DEVELOP

#elif UNITY_IOS && !DELSDK

#elif UNITY_ANDROID
        if (AJO  != null)
            AJO.Call("purchaseAppContent", iap_id,price,currency,source);
#endif
        }
        #endregion




//        public static void SetReYunId(string id)
//        {
//#if UNITY_EDITOR || DEVELOP

//#elif UNITY_IOS && !DELSDK
//            setReYunId(id); //iso 添加
//#elif UNITY_ANDROID
//        if (AJO  != null)
//            AJO.Call("setReYunId",id);
//#endif
//        }
//        /// <summary>
//        ///获取热云id
//        /// </summary>
//        public static string GetReYunId()
//        {
//            string ryID = "";
//#if UNITY_EDITOR || DEVELOP

//#elif UNITY_IOS && !DELSDK
//            ryID = getReYunId(); //iso 添加
//#elif UNITY_ANDROID
//        if (AJO  != null)
//            ryID = AJO.Call<string>("getReYunId");
//#endif
//            return ryID;
//        }
    }
}

